package com.iwic.smartkidsdaycares;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class SplashScreen extends Activity {
	private static String TAG="Splash Screen";
	private static String DB_PATH = "/data/data/com.iwic.smartkidsdaycares/databases/smartkids.db";
	Handler mHandler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);
		try {
			File file = new File(DB_PATH);
			if (!file.exists()){
				copyDataBase();
			}
			
		} catch (Exception ex){
			ex.printStackTrace();
		}
		final Activity splash = this;
		mHandler.postDelayed(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Intent intent= new Intent(splash,MainActivity.class);
				startActivity(intent);
				overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
			}
			
		}, 2000);
	}
	private void copyDataBase() throws IOException{
		 
		//Open your local db as the input stream
		InputStream myInput = getAssets().open("db/smartkids.db");
		 
		// Path to the just created empty db
		String outFileName = DB_PATH ;
		 
		//Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(outFileName);
		 
		//transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer))>0){
		myOutput.write(buffer, 0, length);
		}
		 
		//Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();
		Log.d(TAG,"Copy database success!"); 
		}
}
